package org.abettor.pushbox.activity;

import java.io.File;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.download.DownloadMap;
import org.abettor.pushbox.map.Boxmap;
import org.abettor.pushbox.map.BoxmapUtil;
import org.abettor.pushbox.upload.UploadMap;
import org.abettor.pushbox.R;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;

/**
 * 网络推箱子的activity
 * @author jzh
 */
public class NetPushBoxMapActivity extends AbstractPushBoxActivity {
	private final int FULL_UNFULL_SCREEN_ID = Menu.FIRST;// 全屏
	private final int RESTART_GAME = Menu.FIRST + 1;// 重新开始
	private final int PRE_STEP = Menu.FIRST + 2;// 上一步的操作
	private final int SHOCK_UNSHOCK_ID = Menu.FIRST + 3;// 打开按键反馈
	private int map_id = -1;// 地图的id
	private static final int DELAY = 300;
	private Handler mHandler = new Handler();
	private UploadMap uploadBean = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// 获得地图的id
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");
		uploadBean = new UploadMap(this);
		super.onCreate(savedInstanceState);
	}

	/**
	 * 推箱子成功时的操作，弹出对话框询问是否要标记为解决
	 */
	@Override
	protected void successOperation() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				AlertDialog.Builder builder = new AlertDialog.Builder(
						NetPushBoxMapActivity.this);
				AlertDialog dialog = builder.create();
				dialog.setTitle(R.string.push_map_success);
				dialog.setMessage(NetPushBoxMapActivity.this
						.getText(R.string.net_map_resolved));
				dialog.setButton(
						DialogInterface.BUTTON_POSITIVE,
						NetPushBoxMapActivity.this.getText(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								uploadBean.markNetMapResolved(map_id);
							}
						});
				dialog.setButton(DialogInterface.BUTTON_NEGATIVE,
						NetPushBoxMapActivity.this
								.getText(android.R.string.cancel),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
							}
						});
				dialog.show();
			}
		}, DELAY);
	}

	@Override
	protected String getStepFileName() {
		String filepath = Config.getNetBasePath() + File.separator
				+ String.valueOf(map_id) + ".step";
		return filepath;
	}

	@Override
	protected Boxmap initTheBoxMap() {
		DownloadMap down = new DownloadMap(this);
		byte [] buf = down.readMapFromNetOrLocal(map_id);
		m_map = BoxmapUtil.paraArrayToMap(buf);// 生成地图
		return m_map;
//		String filepath = Config.getNetBasePath() + File.separator
//				+ String.valueOf(map_id) + ".map";
//		try {
//			byte[] buf = FileUtil.readFileByPath(filepath);
//			m_map = BoxmapUtil.paraArrayToMap(buf);// 生成地图
//			return m_map;
//		} catch (IOException e) {
//			return null;
//		}
	}

	/**
	 * 创建系统菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		if (m_pushConfig.getFullScreenOn() == 0) {// 是否打开全屏
			menu.add(0, FULL_UNFULL_SCREEN_ID, 0, R.string.full_screen)
					.setIcon(R.drawable.full_screen_on);
		} else {
			menu.add(0, FULL_UNFULL_SCREEN_ID, 0, R.string.un_full_screen)
					.setIcon(R.drawable.full_screen_off);
		}
		menu.add(0, RESTART_GAME, 1, R.string.restart_game).setIcon(
				R.drawable.reset);
		menu.add(0, PRE_STEP, 2, R.string.pre_step).setIcon(R.drawable.go_back);
		if (m_pushConfig.getShakeOn() == 0) {
			menu.add(0, SHOCK_UNSHOCK_ID, 3, R.string.shock_open).setIcon(
					R.drawable.shaken_on);
		} else {
			menu.add(0, SHOCK_UNSHOCK_ID, 3, R.string.shock_close).setIcon(
					R.drawable.shaken_off);
		}
		return result;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		MenuItem full = menu.findItem(FULL_UNFULL_SCREEN_ID);
		if (m_pushConfig.getFullScreenOn() == 0) {
			full.setIcon(R.drawable.full_screen_on);
			full.setTitle(R.string.full_screen);
		} else {
			full.setIcon(R.drawable.full_screen_off);
			full.setTitle(R.string.un_full_screen);
		}
		MenuItem shock = menu.findItem(SHOCK_UNSHOCK_ID);
		if (m_pushConfig.getShakeOn() == 0) {
			shock.setIcon(R.drawable.shaken_on);
			shock.setTitle(R.string.shock_open);
		} else {
			shock.setIcon(R.drawable.shaken_off);
			shock.setTitle(R.string.shock_close);
		}
		return super.onPrepareOptionsMenu(menu);
	}

	/**
	 * 系统菜单执行的操作
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case FULL_UNFULL_SCREEN_ID:// 全屏
			fullScreen();
			break;
		case RESTART_GAME:// 重新开始游戏
			restart();
			break;
		case PRE_STEP:// 上一步操作
			revert();
			break;
		case SHOCK_UNSHOCK_ID:// 打开按键反馈
			onShock();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	@Override
	public void onBackPressed(){
		super.onBackPressed();
	}
}
